#include "PlayState.h"
#include <Ogre.h>

PlayState::PlayState(){
    mSDwon=0;
    mSpeed=400;
    mMove=0;
    mTranslateVector=Ogre::Vector3::ZERO;
}

PlayState::~PlayState(){
    while(!mEntities.empty())
        delete mEntities.back();

}


void PlayState::init(){

    GameState::init();
    //mRoot = Ogre::Root::getSingletonPtr();
    //mSceneManager = mRoot->getSceneManager("MainSceneManager");
    //mInputManager = InputManager::getSingletonPtr();
    mCamera = mSceneManager->createCamera("PlayCamera");
    mCamera->setPosition(Ogre::Vector3(0,40,210));
    mCamera->lookAt(Ogre::Vector3::ZERO);
    mViewport = mRoot->getAutoCreatedWindow()->addViewport(mCamera);
    mEntities.push_back(mSceneManager->createEntity("Head","ogrehead.mesh"));
    node = mSceneManager->createSceneNode("Nodo1");
    mSceneManager->getRootSceneNode()->addChild(node);
    node->attachObject(mEntities.back());
    mSceneManager->setAmbientLight(Ogre::ColourValue::White);


}

void PlayState::exit()
{
    mRoot->getAutoCreatedWindow()->removeAllViewports();
    GameState::exit();
}

bool PlayState::frameStarted(const Ogre::FrameEvent &evt)
{
    // TEST
    mInputManager->capture();

    Ogre::Radian radians(Ogre::Degree(10));
    if(mSDwon)
           return false;
    if(mMove)
        mTranslateVector.x+=mSpeed*evt.timeSinceLastEvent;
    else
        mTranslateVector=0;
    radians *= evt.timeSinceLastFrame;
    node->rotate(Ogre::Vector3::UNIT_Y, radians);
    std::cout<<mTranslateVector.x<<std::endl;
    node->translate(mTranslateVector,Ogre::Node::TS_LOCAL);

    return true;
    // END TEST
}

bool PlayState::keyPressed(const OIS::KeyEvent &e){
    if(mInputManager->getKeyboard()->isKeyDown(OIS::KC_U))
        mMove=1;
    if(mInputManager->getKeyboard()->isKeyDown(OIS::KC_ESCAPE))
        mSDwon=1;
    return true;
}

bool PlayState::keyReleased(const OIS::KeyEvent &e){
    if(!mInputManager->getKeyboard()->isKeyDown(OIS::KC_U))
        mMove=0;
    return true;
}

bool PlayState::mouseMoved(const OIS::MouseEvent &e){ std::cout<<"KList"<<std::endl;return true;}

bool PlayState::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id){return true;}

bool PlayState::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id){return true;}
